package com.luhuiguo.cms.server.handler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Component;

import com.luhuiguo.cms.server.service.AttachmentService;
import com.luhuiguo.cms.server.util.MapperUtils;
import com.luhuiguo.cms.shared.model.AttachmentModel;
import com.luhuiguo.cms.shared.rpc.GetAttachment;
import com.luhuiguo.cms.shared.rpc.GetAttachmentResult;
import com.luhuiguo.gwt.dispatcher.server.ActionHandler;
import com.luhuiguo.gwt.dispatcher.server.ExecutionContext;
import com.luhuiguo.gwt.dispatcher.shared.exception.ActionException;

@Component
public class GetAttachmentHandler implements ActionHandler<GetAttachment, GetAttachmentResult> {

    @Autowired
    private AttachmentService attachmentService;

    @Override
    @PreAuthorize("isAuthenticated()")
    public GetAttachmentResult execute(GetAttachment action, ExecutionContext context) throws ActionException {
        AttachmentModel model = MapperUtils.map(attachmentService.getAttachment(action.get()), AttachmentModel.class);
        return new GetAttachmentResult(model);
    }

    @Override
    public Class<GetAttachment> getActionType() {
        return GetAttachment.class;
    }

    @Override
    public void rollback(GetAttachment action, GetAttachmentResult result, ExecutionContext context) throws ActionException {

    }

}
